import { Background } from "./background/background";
import { Enemy } from "./enemy/enemy";
import { ResourceLoader } from "./base/resourceLoader"

const ctx = canvas.getContext("2d");
let background = [];
let enemy = [];

/**
 * 游戏主函数                                               
 */
export default class Main {

  constructor() {
    let rl = ResourceLoader.getInstance();
    rl.onLoad(() => this.init());
  }

  
  init() {
    background[0] = new Background();
    background[1] = new Background();
    background[1].y = -window.innerHeight;

    // 初始化敌人
    enemy[0] = new Enemy();

    //
    canvas.addEventListener("touchstart", (e) => {console.log(e)});

    this.run();
  }

  backgroundAutoRun() {
    for(let i = 0; i < background.length; i++) {
      background[i].addY(2);
      background[i].drawToCanvas(ctx);
      if (background[i].y >= window.innerHeight) {
        background[i].y = -window.innerHeight + 2;
      }
    }
  }

  run() {
      ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
      this.backgroundAutoRun();
      enemy[0].drawToCanvas(ctx);
      enemy[0].addY(4);
      let timer = requestAnimationFrame(() => this.run());
  }
}
